package com.azaias.dfs.client;


import com.azarias.dfs.namenode.rpc.model.MkdirRequest;
import com.azarias.dfs.namenode.rpc.model.MkdirResponse;
import com.azarias.dfs.namenode.rpc.model.ShutdownRequest;
import com.azarias.dfs.namenode.rpc.model.ShutdownResponse;
import com.azarias.dfs.namenode.rpc.service.NameNodeServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;

/**
 * @description: 文件操作
 * @author: Azarias_Jx
 * @date: 2021/12/31 14:03
 */
public class FileSystemImpl implements FileSystem{
    private NameNodeServiceGrpc.NameNodeServiceBlockingStub namenode_rpc;

    //注册服务器的域名和端口用以rpc调用
    private final static String NAMENODE_HOSTNAME="localhost";
    private final static Integer NAMENODE_PORT=50070;



    public FileSystemImpl() {
        ManagedChannel channel = NettyChannelBuilder
                .forAddress(NAMENODE_HOSTNAME, NAMENODE_PORT)
                .negotiationType(NegotiationType.PLAINTEXT)
                .build();
        this.namenode_rpc = NameNodeServiceGrpc.newBlockingStub(channel);
    }

    @Override
    public void mkdir(String path) {
         MkdirRequest request = MkdirRequest.newBuilder().setPath(path).build();

         MkdirResponse response = this.namenode_rpc.mkdir(request);

        System.out.println("创建目录的响应：" + response.getStatus());

    }

    @Override
    public void showdown(String code) {
        ShutdownRequest request = ShutdownRequest.newBuilder().setCode(1).build();

        ShutdownResponse response = this.namenode_rpc.shutdown(request);

        System.out.println("优雅关闭响应：" + response.getStatus());
    }
}
